package com.sc.byahi.user.client.components;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.MultiWordSuggestOracle;
import com.google.gwt.user.client.ui.SuggestBox;
import com.google.gwt.user.client.ui.SuggestOracle;
import com.google.gwt.user.client.ui.SuggestOracle.Suggestion;
import com.sc.byahi.user.client.ICleaner;
import com.sc.byahi.user.client.UIUtil;

public class SuggestSelectionList extends Composite implements ICleaner{
	private SuggestBox sbSuggestBox;
	final ListBox cbSelected = new ListBox();
	private FlexTable ft = new FlexTable();
	private Set<String> selectedEntries = new TreeSet<String>();
	int maximum = 10;
	
	public SuggestSelectionList(List<String> selected, List<String> oracleEntries) {
		this(oracleEntries);
		this.selectedEntries.addAll(selected);
		refreshSelectBox();
	}
	public void setOptionsData(List<String> optionsData) {
		this.selectedEntries.clear();
		this.selectedEntries.addAll(optionsData);
		refreshSelectBox();
	}

	@Override
	public void clean() {
		cbSelected.clear();
	}

	/**
	 * @wbp.parser.constructor
	 */
	public SuggestSelectionList(List<String> oracleEntries) {
		initWidget(ft);
		cbSelected.setStyleName("suggest-select-list");
		cbSelected.setVisibleItemCount(5);

		final MultiWordSuggestOracle suggestOracle = new MultiWordSuggestOracle();
		suggestOracle.addAll(oracleEntries);
		sbSuggestBox = new SuggestBox(suggestOracle);
		sbSuggestBox.setStyleName("suggest-select-text");
		sbSuggestBox.addSelectionHandler(new SelectionHandler<SuggestOracle.Suggestion>() {
			@Override
			public void onSelection(SelectionEvent<Suggestion> event) {
				if (selectedEntries.size() == maximum){
					UIUtil.showDialog(Arrays.asList("You can select maximum "+maximum+" entries."), "Limit reached.");
					return;
				}
				selectedEntries.add(sbSuggestBox.getText());
				sbSuggestBox.setText("");
				refreshSelectBox();
			}
		});

		cbSelected.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				int index = cbSelected.getSelectedIndex();
				if (index != -1) {
					String itemText = cbSelected.getItemText(index);
					cbSelected.removeItem(index);
					selectedEntries.remove(itemText);
					sbSuggestBox.setText("");
					refreshSelectBox();
				}
			}
		});

		ft.setWidget(0, 0, sbSuggestBox);
		ft.setWidget(1, 0, cbSelected);

	}
	private void refreshSelectBox() {
		cbSelected.clear();
		for (String country : selectedEntries) {
			cbSelected.addItem(country);
		}
	}
	public List<String> getSelectedItems() {
		List<String> list = new ArrayList<String>();
		list.addAll(selectedEntries);
		return list;
	}
}
